package com.jindu.producer.finance.controller;

import com.jindu.kernel.page.AlphaPage;
import com.jindu.kernel.result.JsonResult;
import com.jindu.kernel.utils.AlphaBeanUtils;
import com.jindu.kernel.utils.DateUtils;
import com.jindu.kernel.utils.IdGenerator;
import com.jindu.producer.finance.jpa.MediaJpa;
import com.jindu.producer.finance.jpa.ReportJpa;
import com.jindu.producer.finance.model.FExperEntity;
import com.jindu.producer.finance.model.FMediaEntity;
import com.jindu.producer.finance.model.FReportEntity;
import com.jindu.producer.finance.model.FShareEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jarvis
 * @date 2018/10/18
 */
@Api(description = "众测报告管理", tags = "ReportController", basePath = "/report")
@RestController
@RequestMapping(value = "/report",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ReportController {

    @Autowired
    ReportJpa reportJpa;

    @Autowired
    MediaJpa mediaJpa;


    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据报告的ID，查询报告的信息", notes = "返回单个评测报告的信息")
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public JsonResult<FReportEntity> get(@RequestParam String id){
        return JsonResult.getSuccessResult(reportJpa.findById(id));
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据ID，查询体验用户的列表信息", notes = "返回单个体验用户的列表信息")
    @RequestMapping(value = "getByProductId",method = RequestMethod.GET)
    public JsonResult<AlphaPage<FReportEntity>> getByProductId(@RequestParam(value = "pageNo",defaultValue = "0") Integer pageNo,
                                                               @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                                               @RequestParam String productId){

        Specification querySpecifi = (Specification<FReportEntity>) (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if(productId != null){
                predicates.add(criteriaBuilder.equal(root.get("productId"),productId));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");

        Page<FReportEntity> entities=reportJpa.findAll(querySpecifi,new PageRequest(pageNo, pageSize,sort));
        for (FReportEntity m : entities) {
            List<FMediaEntity> mediaEntity = mediaJpa.findByReportId(m.getId());
            m.setEntities(mediaEntity);
        }

        return new AlphaPage<>(entities).toJsonResult();
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "保存评测报告的信息", notes = "保存评测报告的信息")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public JsonResult<FReportEntity> save(@RequestBody FReportEntity reportEntity){

        FReportEntity fReportEntity=reportJpa.findById(reportEntity.getId());

        if(fReportEntity == null){
            reportEntity.setId(IdGenerator.uuid());
            reportEntity.setCreateTime(new Timestamp(DateUtils.getCurrentTime()));
            reportEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            return JsonResult.getSuccessResult(reportJpa.saveAndFlush(reportEntity));
        }else {
            reportEntity.setModifyTime(new Timestamp(DateUtils.getCurrentTime()));
            BeanUtils.copyProperties(reportEntity,fReportEntity, AlphaBeanUtils.getNullPropertyNames(reportEntity));
            return JsonResult.getSuccessResult(reportJpa.saveAndFlush(fReportEntity));
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "删除评测报告的信息", notes = "根据ID,删除数据")
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public JsonResult<Integer> delete(@RequestBody String id){
        return JsonResult.getSuccessResult(reportJpa.deleteById(id));
    }







}
